﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_BuMenBiaoService : It_BuMenBiaoService
    {
        public readonly MyContext _Context;

        public t_BuMenBiaoService(MyContext context)
        {
            _Context = context;
        }

        public t_BuMenBiao Add(t_BuMenBiao t_BuMenBiao)
        {
            _Context.t_BuMenBiao.Add(t_BuMenBiao);
            _Context.SaveChanges();
            return  _Context.t_BuMenBiao.FirstOrDefault(x => x.BeiZhu1 == t_BuMenBiao.BeiZhu1 && x.BeiZhu2 == t_BuMenBiao.BeiZhu2 && x.BeiZhu3 == t_BuMenBiao.BeiZhu3 && x.BeiZhu4 == t_BuMenBiao.BeiZhu4 && x.BeiZhu5 == t_BuMenBiao.BeiZhu5 && x.BeiZhu6 == t_BuMenBiao.BeiZhu6 && x.BeiZhu7 == t_BuMenBiao.BeiZhu7 && x.BeiZhu8 == t_BuMenBiao.BeiZhu8 && x.BeiZhu9 == t_BuMenBiao.BeiZhu9 && x.BeiZhu10 == t_BuMenBiao.BeiZhu10 && x.BuMenMingCheng == t_BuMenBiao.BuMenMingCheng && x.GongSiId == t_BuMenBiao.GongSiId && x.ZhanQuZhuangTai == t_BuMenBiao.ZhanQuZhuangTai && x.ZhuangTai == t_BuMenBiao.ZhuangTai);
        }

        public bool Delete(int id)
        {
            throw new NotImplementedException();
        }

        public t_BuMenBiao GetById(int id)
        {
            return  _Context.t_BuMenBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_BuMenBiao> GetList()
        {
            return  _Context.t_BuMenBiao.Include(x => x.t_GongSiBiao).ToList();
        }
        public string ReturnWhere(t_BuMenBiao t_BuMenBiao)
        {
            throw new NotImplementedException();
        }




        public List<t_BuMenBiao> huibangQiYelist(int id)
        {
            return _Context.t_BuMenBiao.Where(x => x.Id == id).ToList();
        }

        public t_BuMenBiao ChaXunYiGe(int id)
        {
            t_BuMenBiao t_BuMenBiao = _Context.t_BuMenBiao.Where(x => x.Id == id).ToList()[0];
            return t_BuMenBiao;
        }

        public string BuMenBiaoAdd(t_BuMenBiao t_BuMenBiao)
        {
            _Context.t_BuMenBiao.Add(t_BuMenBiao);
            if (_Context.SaveChanges() > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }

        public List<t_GongSiBiao> BangDingGongSi()
        {
            return _Context.t_GongSiBiao.Where(x => x.GongSiMingCheng != null).ToList();
        }
        public List<t_RenYuanBiao> BangDingRenYuan()
        {
            return _Context.t_RenYuanBiao.ToList();
        }
        public List<t_BuMenBiao> listshuju()
        {
            t_BuMenBiao bumen = new t_BuMenBiao();
            return _Context.t_BuMenBiao.Include(x => x.t_GongSiBiao).Where(ReturnWhere1(bumen)).ToList();
            //return Helper.ListToJsonAddFenYe(_Context.t_BuMenBiao.Include(x=>x.t_GongSiBiao).Include(x=>x.t_RenYuanBiao).Where(ReturnWhere1(bumen)).ToList(), start, limit);
        }
        public string ReturnWhere1(t_BuMenBiao t_BuMenBiao)
        {
            string returnstr = "Id != 1";
            if (t_BuMenBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_BuMenBiao.BeiZhu1 + "\")";
            }
            if (t_BuMenBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_BuMenBiao.BeiZhu2 + "\")";
            }
            if (t_BuMenBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_BuMenBiao.BeiZhu3 + "\")";
            }
            if (t_BuMenBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_BuMenBiao.BeiZhu4 + "\")";
            }
            if (t_BuMenBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_BuMenBiao.BeiZhu5 + "\")";
            }
            if (t_BuMenBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(\"" + t_BuMenBiao.BeiZhu6 + "\")";
            }
            if (t_BuMenBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(\"" + t_BuMenBiao.BeiZhu7 + "\")";
            }
            if (t_BuMenBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(\"" + t_BuMenBiao.BeiZhu8 + "\")";
            }
            if (t_BuMenBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(\"" + t_BuMenBiao.BeiZhu9 + "\")";
            }
            if (t_BuMenBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(\"" + t_BuMenBiao.BeiZhu10 + "\")";
            }
            if (t_BuMenBiao.BuMenMingCheng != null)
            {
                returnstr = returnstr + " and BuMenMingCheng.Contains(\"" + t_BuMenBiao.BuMenMingCheng + "\")";
            }
            if (t_BuMenBiao.GongSiId > 0)
            {
                returnstr = returnstr + " and GongSiId==" + t_BuMenBiao.GongSiId;
            }
            if (t_BuMenBiao.ZhanQuZhuangTai != null)
            {
                returnstr = returnstr + " and ZhanQuZhuangTai==\"" + t_BuMenBiao.ZhanQuZhuangTai + "\"";
            }
            if (t_BuMenBiao.ZhuangTai != null)
            {
                returnstr = returnstr + " and ZhuangTai ==\"" + t_BuMenBiao.ZhuangTai + "\"";
            }
            return returnstr;
        }

        public string GetListWithFenYe(t_BuMenBiao t_BuMenBiao, int start, int limit, string dir, int column)
        {
            var list = _Context.t_BuMenBiao.Include(x => x.t_GongSiBiao).Where(ReturnWhere1(t_BuMenBiao)).ToList();
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.BuMenMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.BuMenMingCheng).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_GongSiBiao.GongSiMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_GongSiBiao.GongSiMingCheng).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhanQuZhuangTai).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhanQuZhuangTai).ToList();
                    }
                    break;
                case 3:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhuangTai).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhuangTai).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
